package rlyy.framework.netty.servlet;

import rlyy.framework.core.common.utils.UrlMapper;
import lombok.SneakyThrows;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import rlyy.framework.core.servlet.ServletFilterRegistration;
import rlyy.framework.core.servlet.ServletRegistration;

import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.Servlet;

/**
 * 添加ServletContext
 */
public class MyServletContext extends MockServletContext {

    private UrlMapper<rlyy.framework.core.servlet.ServletRegistration> servletUrlMapper = new UrlMapper<>(true);
    private UrlMapper<rlyy.framework.core.servlet.ServletFilterRegistration> filterUrlMapper = new UrlMapper<>(false);

    @SneakyThrows
    @Override
    public rlyy.framework.core.servlet.ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
        MockServletConfig myServletConfig = new MockServletConfig();
        servlet.init(myServletConfig);
        rlyy.framework.core.servlet.ServletRegistration servletRegistration = new ServletRegistration(servletName,servlet,this,servletUrlMapper);
        return servletRegistration;
    }

    @Override
    public FilterRegistration.Dynamic addFilter(String filterName, Filter filter){
        rlyy.framework.core.servlet.ServletFilterRegistration registration = new ServletFilterRegistration(filterName,filter,this,filterUrlMapper);
        return registration;
    }
}
